<?php

namespace app\admin\model;

use app\model\Model;
use DataOps;

class Admin extends Model
{
  public $privilege = 'all';
  public $cids;
  public $id;
  public $db;

  function __construct()
  {
    include 'check.php';
    new Check();

    $this->cids = checkAdminPriv(@$this->privilege); // 判断权限
    $this->db = $GLOBALS['db'];
    $this->id = intval($_GET['id']);
  }

  function index()
  {
    include_once LIB_PATH . 'cls.page.php';

    $pager = new \Page(ADMIN_PAGE_SIZE);
    $pager->handle($GLOBALS['db']->getOne("SELECT COUNT(*) FROM user WHERE u_isadmin = 1"));

    $this->tpl('admin', [
      'db' => $this->db,
      'list_role' => $this->db->getAll("SELECT id as value,r_name as label FROM role"),
      'list' => $GLOBALS['db']->getAll("SELECT * FROM user WHERE u_isadmin = 1 ORDER BY id ASC LIMIT " . $pager->page_start . "," . $pager->page_size),
      'pager' => $pager,
    ]);
  }

  function add()
  {
    $data = \get_post(['u_tname']);
    $res = $this->db->getRow("SELECT * FROM user WHERE u_name = '" . $data['u_name'] . "'");

    if (is_array($res)) {
      alert_back('登录帐号已存在');
    }

    if (!check($data['u_psw'], 'regex', 'strong_password')) {
      alert_back('密码不符合规范');
    }

    if ($data['u_psw'] != $data['u_psw_confirm']) {
      alert_back('效验密码不匹配');
    }

    unset($data['u_psw_confirm']);
    $data['u_psw'] = \psw_hash($data['u_psw']);
    $data['u_enable'] = 1;
    $data['u_isadmin'] = 1;

    if ($id = $GLOBALS['db']->autoExecute('user', $data)) {
      \admin_log('管理员添加 [id:' . $id . ']');
      alert_href($GLOBALS['lang']['msg_success'], M('admin', 'admin'));
    }

    alert_back($GLOBALS['lang']['msg_tryagain']);
  }

  function edit()
  {
    $this->tpl('admin_edit', [
      'db' => $this->db,
      'list_role' => $this->db->getAll("SELECT id as value,r_name as label FROM role"),
      'row' => $this->db->getRow("SELECT * FROM user WHERE id=$this->id")
    ]);
  }

  function edit_action()
  {
    $data = \get_post(['u_psw', 'u_psw_confirm', 'u_tname']);

    if (!empty($data['u_psw'])) {
      if (!check($data['u_psw'], 'regex', 'strong_password')) {
        alert_back('密码不符合规范');
      }

      if (empty($data['u_psw_confirm'])) {
        alert_back('效验密码不能为空');
      }
    }

    if (!empty($data['u_psw_confirm'])) {
      if ($data['u_psw'] != $data['u_psw_confirm']) {
        alert_back('效验密码不匹配');
      }

      unset($data['u_psw_confirm']);
      $data['u_psw'] = \psw_hash($data['u_psw']);
    } else {
      unset($data['u_psw']);
      unset($data['u_psw_confirm']);
    }

    if ($GLOBALS['db']->autoExecute('user', $data, 'UPDATE', "id=$this->id")) {
      \admin_log('管理员编辑 [id:' . $this->id . ']');
      alert_href($GLOBALS['lang']['msg_success'], M('admin', 'admin'));
    }

    alert_back($GLOBALS['lang']['msg_tryagain']);
  }

  function del()
  {
    if ($this->id == 1) {
      alert_back('默认账户不能删除！');
    }


    if ($GLOBALS['db']->exec("DELETE FROM user WHERE id = $this->id")) {
      \admin_log('管理员删除 [id:' . $this->id . ']');
      alert_href($GLOBALS['lang']['msg_success'], M('admin', 'admin'));
    }

    alert_back($GLOBALS['lang']['msg_tryagain']);
  }

  function __destruct()
  {
  }
}